package cn.elebu;


import javax.swing.*;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

/**
 * 打地鼠游戏类
 * @author hz
 */
public class PlayMouse extends Thread {
    /**
     * 窗口
     */
    private JFrame jf;

    /**
     * 计时器组件
     */
    private JLabel timer;

    /**
     * 背景图片组件
     */
    private JLabel bgImg;

    /**
     * 分数组件
     */
    private JLabel score;

    /**
     *  背景图片
     */
    private ImageIcon imageIcon;

    /**
     * 分数
     */
    private int num;

    /**
     * 计时器
     */
    private int time = 60;

    /**
     * 锤子图标
     */
    private Image cz;

    /**
     * 锤子图标2
     */
    private Image czz;

    /**
     * 装老鼠的数组
     */
    private JLabel[] mouses = new JLabel[9];

    /**
     * 老鼠图标
     */
    private ImageIcon mouseBefore;

    /**
     * 老鼠被打图片
     */
    private ImageIcon mouseAfter;

    /**
     * 点击老鼠音效
     */
    private AudioClip audioClip;


    public PlayMouse() {
        // 初始化窗口
        jf = new JFrame();

        // 背景图片
        bgImg = new JLabel();
        imageIcon = new ImageIcon(this.getClass().getResource("resource/background.png"));
        bgImg.setIcon(imageIcon);

        // 分数
        score = new JLabel();
        score.setText(num+"分");
        score.setForeground(Color.BLACK);
        score.setFont(new Font("微软雅黑",0,40));
        score.setBounds(170,40,120,40);

        // 计时器
        timer = new JLabel();
        timer.setText(time+"秒");
        timer.setForeground(Color.BLACK);
        timer.setFont(new Font("微软雅黑",0,40));
        timer.setBounds(530,40,120,40);

        // 设置Windows鼠标
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        cz = new ImageIcon(this.getClass().getResource("resource/hammer.png")).getImage();
        Cursor cursor = toolkit.createCustomCursor(cz, new Point(), "锤子");

        // 给数组装入老鼠
        mouseBefore = new ImageIcon(this.getClass().getResource("resource/mouseBefore.png"));
        for (int i = 0; i < 9; i++) {
            mouses[i] = new JLabel();
            mouses[i].setSize(mouseBefore.getIconWidth(),mouseBefore.getIconHeight());
            mouses[i].addMouseListener(new MouseAdapter() {
                // 添加点击事件
                @Override
                public void mouseClicked(MouseEvent e) {
                    // 每个图片只能点击一次
                    //if (e.getClickCount() == 1){
                        Object source = e.getSource();
                        if (source instanceof JLabel){
                            JLabel label = (JLabel) source;
                            //判断是否不为空
                            if (label.getIcon() != null){
                                // 被锤后设置图片
                                mouseAfter = new ImageIcon(this.getClass().getResource("resource/mouseAfter.png"));
                                label.setIcon(mouseAfter);
                                JApplet.newAudioClip(this.getClass().getResource("resource/jida.WMA")).play();
                                num+=10;
                                score.setText(num+"分");
                            }
                        }
                    //}
                }
                // 鼠标动效设置
                // 按下
                @Override
                public void mousePressed(MouseEvent e) {
                    czz = new ImageIcon(this.getClass().getResource("resource/hammers.png")).getImage();
                    Cursor cursor = toolkit.createCustomCursor(czz, new Point(), "锤子");
                    jf.setCursor(cursor);
                }
                // 释放
                @Override
                public void mouseReleased(MouseEvent e) {
                    jf.setCursor(cursor);
                }
            });
            jf.add(mouses[i]);
        }

        // 给每一个老鼠设置坐标
        mouses[0].setLocation(250,115);
        mouses[1].setLocation(420,115);
        mouses[2].setLocation(600,115);
        mouses[3].setLocation(220,245);
        mouses[4].setLocation(420,245);
        mouses[5].setLocation(630,245);
        mouses[6].setLocation(190,400);
        mouses[7].setLocation(420,400);
        mouses[8].setLocation(660,400);


        // 添加组件
        jf.setCursor(cursor);
        jf.add(score);
        jf.add(timer);
        jf.add(bgImg);

        // 设置标题
        jf.setTitle("打地鼠");
        // 设置图标
        jf.setIconImage(mouseBefore.getImage());
        // 设置宽高
        jf.setBounds(200,100,1024,768);
        // 设置显示窗口
        jf.setVisible(true);
        // 设置不可拉伸
        jf.setResizable(false);
        // 设置关闭窗口 结束线程
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    /**
     * 开启线程
     */
    @Override
    public void run() {
        while(true){
            int i = new Random().nextInt(9);
            JLabel mouse = mouses[i];
            if (mouse.getIcon() == null){
                try {
                    Thread.sleep(200);
                    mouse.setIcon(mouseBefore);
                    Thread.sleep(800);
                    // 如果再显示中 设置为空
                    if (mouse.isShowing()){
                        mouse.setIcon(null);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 倒计时减少
                time--;
                timer.setText(time+"秒");
                if (time == 0){
                    JApplet.newAudioClip(this.getClass().getResource("resource/bgm.wav")).play();
                    JOptionPane.showMessageDialog(null,"恭喜您,您获得了"+num+"分","游戏结束",2);
                    break;
                }
            }
        }
    }

}
